<template>
  <div class="minegoods-model">
    <div class="date-box">
      <!-- <div class="time">2018年12月</div> -->
      <div class="date-select" @click="showDateSelect">
        <div class="time">{{dateTimeVal | fmate}}</div>
        <div class="icon" :class="{'arrow': showDate}"></div>
      </div>
      <div class="allmoney">总计积分 {{recordData.totalScore || 0}}</div>
    </div>
    <div class="integral-container">
      <scroller :on-refresh="refresh" :on-infinite="infinite" v-if="!isInit">
        <div class="record-list" v-if="integralList.length" :data-length="integralList.length"
        :class="{'loaded-data': integralList.length < 5, 'loadmore-data': integralList.length < 10}">
          <div class="record-item" v-for="(item, index) in integralList" :key="index">
            <div class="types">
              <div class="type">{{item.remark}}</div>
              <div class="time">{{item.create_time | formatDate}}</div>
            </div>
            <!-- <div class="status">{{item.status}}</div> -->
            <div class="adds" v-if="item.score>=0">+{{item.score || 0}}</div>
            <div class="reduce" v-else>{{item.score || 0}}</div>
          </div>
        </div>
      </scroller>
    </div>
    <div class="fllow-pay">
      <div class="bet">
        兑<inline-x-number class="custom-number" width="1rem"
        :min="20"
        :max="Number(recordData.totalScore) || 20"
        v-model="getScore"
        @on-change="changeXNumber"
        ></inline-x-number>换
        <span class="money">{{getScore/10}}元</span>
      </div>
      <div class="pay-btn" @click="submitXnumber">确认兑换</div>
    </div>
    <div class="no-datas" v-if="!loading && !integralList.length">
      <img src="../../../assets/imgs/icon-noattention@2x.png" alt="">
      <div class="desc">暂无数据</div>
    </div>
    <datetime
      :show="showDate"
      v-model="dateTimeVal"
      format="YYYY-MM"
      start-date="2018-01-01"
      :end-date="currentDate"
      @on-confirm="dateConfirm"
      @on-cancel="dateCancel"
      year-row="{value}年"
      month-row="{value}月"
    ></datetime>
    <popup v-model="showPopup">
      <div class="record-type">
        <div class="header">
          选择类型
          <span class="icon-del" @click="showPopup=false"></span>
        </div>
        <div class="types">
          <div class="state" v-for="(item, index) in  recordTypes" :key="index"
          :class="{'on':recordType==index}" @click="selectType(item.status, index)">{{item.text}}</div>
        </div>
        <div class="btn" @click="searchRecord">确定</div>
      </div>
    </popup>
  </div>
</template>
<script>
import { mapActions } from 'vuex';

export default {
  components: {
  },
  data () {
    return {
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      isInit: false, // 重置滚动
      showDate: false,
      showPopup: false,
      dateTimeVal: '',
      recordType: 0,
      getScore: 20,
      status: '',
      recordTypes: [
        {
          id: 1,
          status: '',
          text: '全部状态'
        },
        {
          id: 2,
          status: 1,
          text: '支付成功'
        },
        {
          id: 3,
          status: 3,
          text: '处理中'
        }
      ],
      recordData: {},
      integralList: [
        // {
        //   "member_score_id": 1,
        //   "score": "10",
        //   "remark": "签到",
        //   "create_time": "1550000000",
        //   "modify_time": "0"
        // },
        // {
        //   "member_score_id": 2,
        //   "score": "10",
        //   "remark": "签到",
        //   "create_time": "1550995735",
        //   "modify_time": "0"
        // }
      ]
    }
  },
  computed: {
    currentDate () {
      const date = new Date();
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      month = month < 10 ? '0' + month : month;
      day = day < 10 ? '0' + day : day;
      return `${year}-${month}-${day}`
    }
  },
  filters: {
    fmate (val) {
      const _date = val.split('-');
      return `${_date[0]}年${_date[1]}月`;
    }
  },
  created () {
    this.dateTimeVal = this.currentDate.substring(0, 7);
  },
  methods: {
    ...mapActions({
      getMemberScoreList: 'mine/getMemberScoreList',
      setExchangeScore: 'mine/setExchangeScore'
    }),
    refresh (done) { // 下拉
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) { // 上滑
      if (!this.isPull) {
        this.loading = true;
        this.getDatas(done);
      }
    },
    getDatas (done) {
      if (this.loading) {
        const starttime = this.dateTimeVal + '-01 00:00:00';
        const endtime = new Date().getTime();
        const params = {
          page: this.pageNum,
          start_time: new Date(starttime).getTime() / 1000,
          end_time: parseInt(endtime / 1000) // 当前时间
          // status: this.status // 1成功 2失败 3处理中
        }
        this.getMemberScoreList(params).then(res => {
          this.loading = false;
          let listData = res.items || [];
          if (this.isInit) {
            this.isInit = false;
          }
          if (listData.length) {
            if (this.isPull) {
              this.isPull = false;
              this.integralList = [];
            }
            this.pageNum += 1;
            const listDatas = this.integralList;
            this.integralList = listDatas.concat(listData);
            if (!res.isEnd) {
              done && done(true);
            } else {
              done && done(2); ;
            }
          } else {
            done && done(2);
          }
        });
      }
    },
    changeXNumber () {
      console.log(this.getScore);
    },
    submitXnumber () {
      console.log(this.getScore);
      this.setExchangeScore({score: this.getScore}).then(res => {
        if (res) {
          this.$vux.toast.text(`兑换${this.getScore}积分成功`);
          this.getScore = 1;
        }
      });
    },
    selectType (status, index) {
      this.status = status;
      this.recordType = index;
    },
    searchRecord () {
      console.log(this.recordType);
      // 成功...
      this.showPopup = false;
      // this.refresh();
      // this.getDatas(() => {});
    },
    showDateSelect () {
      this.showDate = true;
    },
    dateConfirm () {
      this.showDate = false;
      this.integralList = [];
      this.isInit = true;
      this.refresh();
    },
    dateCancel () {
      this.showDate = false;
    }
  }
}
</script>
<style lang="less" scoped>
  @import '../../../assets/styles/mixins.less';
  .minegoods-model {
    width: 100%;
    position: absolute;
    top: 1.72rem;
    bottom: 1rem;
    background: #ffffff;
    overflow: hidden;
    .date-box {
      // position: fixed;
      // top: 1.72rem;
      width: 100%;
      height: 1.06rem;
      padding: 0.14rem 0.2rem;
      box-sizing: border-box;
      background: #f7f7f7;
      z-index: 10;
      .date-select {
        display: inline-flex;
        align-items: center;
        height: 0.4rem;
        line-height: 0.4rem;
        box-sizing: border-box;
        .time {
          font-size: 0.28rem;
          color: #333333;
        }
        .icon {
          width: 0.24rem;
          height: 0.13rem;
          margin-left: 0.05rem;
          background-size: 0.24rem 0.13rem;
          background-repeat: no-repeat;
          background-image: url('../../../assets/imgs/mine/icon-down@2x.png')
        }
        .arrow {
          transform: rotate(180deg);
          transition: transform 0.4s;
        }
      }
      .allmoney {
        margin-top: 0.1rem;
        font-size: 0.22rem;
        color: #999999;
      }
    }
    .integral-container {
      position: absolute;
      top: 1.06rem;
      bottom: 0;
      width: 100%;
      overflow-y: auto;
      // position: relative;
      // // top: 0.88rem;
      // margin-bottom: 1rem;
      background: #f7f7f7;
    }
    .record-list {
      // position: absolute;
      // top: 1.06rem;
      // bottom: 1rem;
      // width: 100%;
      // overflow-y: auto;
      // // position: relative;
      // // // top: 0.88rem;
      // // margin-bottom: 1rem;
      // background: #ffffff;
      .record-item {
        position: relative;
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 0.96rem;
        padding: 0 0.3rem 0 0.2rem;
        font-size: 0.28rem;
        color: #333333;
        background: #ffffff;
        &::after {
          content: "";
          display: block;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 100%;
          height: 0.02rem;
          background: #d2d2d2;
          transform: scaleY(0.5);
        }
        .types {
          .type {
            margin-bottom: 0.1rem;
          }
          .time {
            font-size: 0.22rem;
            color: #999999;
          }
        }
        .adds {
          font-size: 0.34rem;
          color: #D44743;
        }
        .reduce {
          font-size: 0.34rem;
          color: #3F8FEB ;
        }
      }
    }
    .fllow-pay {
      position: fixed;
      bottom: 0;
      width: 100%;
      height: 1rem;
      line-height: 1rem;
      display: flex;
      background: rgba(255, 255, 255, 0.9);
      .bet {
        flex: 1;
        padding-left: 0.18rem;
        .custom-number {
          vertical-align: middle;
        }
      }
      .money {
        font-size: 0.30rem;
        color: #D44743;
      }
      .pay-btn {
        width: 2.6rem;
        font-size: 0.34rem;
        color: #ffffff;
        text-align: center;
        background: #D44743;
      }
    }
    .no-datas {
      position: fixed;
      width: 100%;
      top: 2.8rem;
      bottom: 0;
      height: 100%;
      text-align: center;
      background: #ffffff;
      img {
        display: block;
        width: 4.15rem;
        height: 3.22rem;
        margin: 0 auto;
        padding-top: 0.8rem;
      }
      .desc {
        margin-top: 0.3rem;
        font-size: 0.22rem;
        color: #919191;
      }
    }
    .vux-datetime {
      display: none;
    }
    .record-type {
      .header {
        position: relative;
        height: 0.8rem;
        line-height: 0.8rem;
        font-size: 0.32rem;
        color: #333333;
        text-align: center;
        &::after {
          content: "";
          display: block;
          position: absolute;
          bottom: 0;
          right: 0;
          width: 100%;
          height: 0.02rem;
          background: #d2d2d2;
          transform: scaleY(0.5);
        }
        .icon-del {
          position: absolute;
          right: 0.2rem;
          top: 0;
          width: 0.3rem;
          height: 0.3rem;
          padding: 0.25rem;
          background-image: url('../../../assets/imgs/icon-close@2x.png');
          background-size: 0.3rem 0.3rem;
          background-position: 50% 50%;
          background-repeat: no-repeat;
        }
      }
      .types {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.3rem 0.2rem;
        .state {
          width: 32.3%;
          height: 0.8rem;
          line-height: 0.8rem;
          font-size: 0.28rem;
          color: #333333;
          text-align: center;
          border: 1px solid #cd6c69;
          border-radius: 0.06rem;
          box-sizing: border-box;
          &.on {
            color: #ffffff;
            background: #d44743;
          }
        }
      }
      .btn {
        width: 100%;
        height: 1rem;
        line-height: 1rem;
        font-size: 0.34rem;
        color: #ffffff;
        text-align: center;
        background: #d44743;
      }
    }
  }
</style>
